<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
	<head>
		<meta charset="utf-8">
		<title></title>
		<meta name="viewport" content="width=device-width, initial-scale=1" />
		<script th:src="@{/static/js/jquery.min.js}" type="text/javascript" charset="utf-8"></script>
		<script th:src="@{/static/js/afquery.js}" type="text/javascript" charset="utf-8"></script>
		<script th:src="@{/static/layui/layui.all.js}" type="text/javascript" charset="utf-8"></script>
		<link rel="stylesheet" type="text/css" th:href="@{/static/layui/css/layui.css}" />
		<link rel="stylesheet" type="text/css" th:href="@{/static/css/Common.css}" />
	</head>
	<body>
		<div class="layui-row" id="Main" style="height: 100%;overflow: hidden;">
			<!-- 顶部区 -->
			<div th:replace =" ~{A_Common::Top_Menu}"></div>
			
			<!-- 左侧菜单区 -->
			<div th:replace =" ~{A_Common::Left_Menu}"></div>
			
			<!-- 右侧内容区 -->
			<!-- ===========================================权限管理================================================== -->
			<div th:fragment="Authority_Manager" class="layui-col-xs8 layui-col-md10 layui-row" id="right_Content">
				<fieldset class="layui-elem-field site-demo-button" style="padding: 5%;">
					<legend>权限管理</legend>
					  <div class="layui-tab">
					    <ul class="layui-tab-title">
					      <li onclick="M.tab(this)">管理员</li>
					      <li onclick="M.tab(this)">教师</li>
					      <li onclick="M.tab(this)">学生</li>
					    </ul>
					    <div class="layui-tab-content">
					      <div class="layui-tab-item" id="AdminTable">
					      	<table class="layui-table">
					      		<colgroup>
					      			<col width="40">
									<col width="60">
									<col>
					      		</colgroup>
					      		<thead>
					      			<tr>
					      				<th><input type="checkbox" checked="true" onclick="" disabled="disabled" /></th>
					      				<th>序号</th>
					      				<th>功能权限</th>
					      			</tr>
					      		</thead>
					      		<tbody>
					      			<tr th:each="row : ${datas.leftMenu}">
					      				<td><input type="checkbox" checked="true" name="selectID" disabled="disabled" /></td>
					      				<td th:idd="${row.id}" th:text="${rowStat.index}+1">1</td>
					      				<td th:text="${row.FunctionName}">阿里巴巴</td>
					      			</tr>
					      		</tbody>
					      	</table>
					      	<!-- 								<div class="nothing">
					      							没有查询到有用数据
					      						</div> -->
					      </div>
						<div class="layui-tab-item" id="TeacherTable">
					      	<table class="layui-table">
					      		<colgroup>
					      			<col width="40">
									<col width="60">
									<col>
					      		</colgroup>
					      		<thead>
					      			<tr>
					      				<th><input type="checkbox" onclick="" disabled="disabled" /></th>
					      				<th>序号</th>
					      				<th>功能权限</th>
					      			</tr>
					      		</thead>
					      		<tbody>
					      			<tr th:each="row : ${datas.leftMenu}">
					      				<td th:if="${row.isTeacher == true}"><input type="checkbox" checked="true" name="selectID" disabled="disabled" /></td>
										<td th:if="${row.isTeacher != true}"><input type="checkbox" name="selectID" disabled="disabled" /></td>
					      				<td th:idd="${row.id}" th:text="${rowStat.index}+1">1</td>
					      				<td th:text="${row.FunctionName}">阿里巴巴</td>
					      			</tr>
					      		</tbody>
					      	</table>
					      	<!-- 								<div class="nothing">
					      							没有查询到有用数据
					      						</div> -->
							<div class="layui-row">
								<div class="layui-btn-group layui-col-md-offset9">
									<button type="button" class="layui-btn" onclick="M.OpenUpdate(this)">编辑</button>
									<button type="button" class="layui-btn" onclick="M.Save(this)">保存</button>
								</div>
							</div>
					      </div>
					      <div class="layui-tab-item" id="StudentTable">
							<table class="layui-table">
								<colgroup>
									<col width="40">
								<col width="60">
								<col>
								</colgroup>
								<thead>
									<tr>
										<th><input type="checkbox" onclick="" disabled="disabled" /></th>
										<th>序号</th>
										<th>功能权限</th>
									</tr>
								</thead>
								<tbody>
									<tr th:each="row : ${datas.leftMenu}">
										<td th:if="${row.isTeacher == true}"><input type="checkbox" checked="true" name="selectID" disabled="disabled" /></td>
										<td th:if="${row.isTeacher != true}"><input type="checkbox" name="selectID" disabled="disabled" /></td>
										<td th:idd="${row.id}" th:text="${rowStat.index}+1">1</td>
										<td th:text="${row.FunctionName}">阿里巴巴</td>
									</tr>
								</tbody>
							</table>
					        	<!-- 								<div class="nothing">
					        							没有查询到有用数据
					        						</div> -->
					      	<div class="layui-row">
					      		<div class="layui-btn-group layui-col-md-offset9">
					      			<button type="button" class="layui-btn" onclick="M.OpenUpdate(this)">编辑</button>
					      			<button type="button" class="layui-btn" onclick="M.Save(this)">保存</button>
					      		</div>
					      	</div>
					        </div>
					    </div>
					  </div>
				</fieldset>
			</div>
			
		</div>
	</body>
	<!-- 初始化点击事件 -->
	<script th:replace =" ~{A_Common::Init_Click}"></script>
	<script th:inline="javascript">
	//Demo
	layui.use('form', function(){
	  var form = layui.form;
	  form.render();
	  //监听提交
	  form.on('submit(formDemo)', function(data){
	    layer.msg(JSON.stringify(data.field));
	    return false;
	  });
	  
	});
	
	var isUpdate = false;
	//打开权限修改
	M.OpenUpdate = function(e){
		let table = $(e).parent().parent().prev();
		let trs = $(table).find("tbody tr");
		for(let i = 0;i < trs.length;i++){
			$(table).find("thead tr th input").attr("disabled",false);
			$($(trs.get(i)).children().children()).attr("disabled",false);
		}
		isUpdate = true;
	}
	
	//保存修改权限
	M.Save = function(e){
		if(!isUpdate){
			return;
		}
		let table = $(e).parent().parent().prev();
		let trs = $(table).find("tbody tr");
		var array = new Array();
		for(let i = 0;i < trs.length;i++){
			
			if($($(trs.get(i)).children().children()).is(":checked")){
				let id = $(trs.get(i)).children().next().attr("idd");
				array.push(id);
			}
		}	//教师权限
		if($(table).parent().attr("id") == "TeacherTable"){
			Af.rest([[@{author_saveTeacher.do}]],array,function(data){
				layer.open({
					title: "信息",
					content: '成功！',
					offset: 't',
					time: 500,
					success: function(layero, index){
						setTimeout(function(){location.reload();},600);
					  } 
				});
			});
		}else{		//学生权限
			Af.rest([[@{author_saveStudent.do}]],array,function(data){
				layer.open({
					title: "信息",
					content: '成功！',
					offset: 't',
					time: 500,
					success: function(layero, index){
						setTimeout(function(){location.reload();},600);
					  } 
				});
			});
		}
		$($(trs).find("input")).attr("disabled",true);
		$(table).find("thead tr th input").attr("disabled",true);
		isUpdate = false;
	}
	
	var page = sessionStorage.getItem("page");
	M.init = function(){
		$(tab_titles).removeAttr("class");
		$(tab_contents).hide();
		if(Number(page) >= 0){
			$(tab_titles).removeAttr("class");
			$(tab_contents).hide();
			
			switch(Number(page)){
				case 0:
					sessionStorage.setItem("page",0);
					$(".layui-tab-title li:eq(0)").attr("class","layui-this");
					$(".layui-tab-content .layui-tab-item:eq(0)").show();
				break;
				case 1:
					sessionStorage.setItem("page",1);
					var s = $(".layui-tab-title li:eq(1)").attr("class","layui-this");
					var m = $(".layui-tab-content .layui-tab-item:eq(1)").show();
				break;
				case 2:
					sessionStorage.setItem("page",2);
					$(".layui-tab-title li:eq(2)").attr("class","layui-this");
					$(".layui-tab-content .layui-tab-item:eq(2)").show();
				break;
				case 3:
					sessionStorage.setItem("page",3);
					$(".layui-tab-title li:eq(3)").attr("class","layui-this");
					$(".layui-tab-content .layui-tab-item:eq(3)").show();
				break;
				default: 
				       sessionStorage.setItem("page",0);
				       $(".layui-tab-title li:eq(0)").attr("class","layui-this");
				       $(".layui-tab-content .layui-tab-item:eq(0)").show();
				break;
			}	
		}else{
			sessionStorage.setItem("page",0);
			$(".layui-tab-title li:eq(0)").attr("class","layui-this");
			$(".layui-tab-content .layui-tab-item:eq(0)").show();
			}
		}();
	</script>
	
</html>
